package tcp

import (
	"net"

	"gitee.com/hasika/common-server/conn/fd"
	"gitee.com/hasika/common-server/conn/impl_base"
	"gitee.com/hasika/common-server/conn/inet"
)

type Connection struct {
	TCPConn *net.TCPConn
	*impl_base.Connection
}

func (t *Connection) GetRemote() net.Addr {
	return t.TCPConn.RemoteAddr()
}

func (t *Connection) InitFd() {
	t.SetFd(fd.SocketFD(t.TCPConn))
}

func (t *Connection) CloseConn() error {
	return t.TCPConn.Close()
}

func NewTcpConnection(TCPConn *net.TCPConn) *Connection {
	return &Connection{TCPConn: TCPConn, Connection: impl_base.NewBaseConnection()}
}

func (t *Connection) GetConnectionType() inet.ConnectionType {
	return inet.Tcp
}

func (t *Connection) LowerWrite(data []byte) error {
	return t.WriteWithLimit(t.TCPConn, data)
}
func (t *Connection) Read(p []byte) (n int, err error) {
	defer func() {
		t.RecordRead(n)
	}()
	return t.TCPConn.Read(p)
}
